local MakePlayerCharacter = require "prefabs/player_common"

local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
    Asset("ANIM", "anim/assn_567.zip"),
    Asset("ANIM", "anim/ghost_assn_567_build.zip"),
}

-- Your character's stats
TUNING.ASSN_567_HEALTH = 100
TUNING.ASSN_567_HUNGER = 100
TUNING.ASSN_567_SANITY = 100

-- Custom starting inventory
TUNING.GAMEMODE_STARTING_ITEMS.DEFAULT.ASSN_567 = {
    "assn_jiandao",
}

local start_inv = {}
for k, v in pairs(TUNING.GAMEMODE_STARTING_ITEMS) do
    start_inv[string.lower(k)] = v.ASSN_567
end
local prefabs = FlattenTree(start_inv, true)

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when not a ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "assn_567_speed_mod", 1.1)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
    inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "assn_567_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- 攻击增加体力值
local function onstaminaup(inst, data)
    if inst.components.stamina then
        local max = inst.components.stamina.max
        local current = inst.components.stamina.current
        if current < max then
            inst.components.stamina:SetCurrent(current + 1)
        end
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst)
    -- Minimap icon
    inst.MiniMapEntity:SetIcon("assn_567.tex")

    inst:AddTag("assn_567")
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
    -- Set starting inventory
    inst.starting_inventory = start_inv[TheNet:GetServerGameMode()] or start_inv.default

    -- choose which sounds this character will play
    inst.soundsname = "wilson"

    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    -- inst.talker_path_override = "dontstarve_DLC001/characters/"

    -- Stats	
    inst.components.health:SetMaxHealth(TUNING.ASSN_567_HEALTH)
    inst.components.hunger:SetMax(TUNING.ASSN_567_HUNGER)
    inst.components.sanity:SetMax(TUNING.ASSN_567_SANITY)

    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1.5

    -- 添加升级组件
    inst:AddComponent("expbooster")

    -- 添加体力组件
    inst:AddComponent("stamina")

    -- 饮食习惯,可以吃噩梦燃料
    local caneat = { FOODGROUP.OMNI, FOODTYPE.NIGHTMARE }
    inst.components.eater:SetDiet(caneat, caneat)

    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE

    inst:ListenForEvent("onhitother", onstaminaup)

    inst.OnLoad = onload
    inst.OnNewSpawn = onload
end

return MakePlayerCharacter("assn_567", prefabs, assets, common_postinit, master_postinit, prefabs)
